/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.erp.inv.input.dao.hibernate;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.erp.inv.input.dao.model.InvInputLineVO;
import com.erp.masterdata.common.model.BaseModel;
import org.redragon.framework.annotation.Permissions;
import org.redragon.framework.annotation.Permissions.PermissionType;
import org.redragon.framework.annotation.SqlParam;
import org.redragon.framework.hibernate.DaoSupport;
import org.redragon.framework.hibernate.model.Pages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.erp.inv.input.dao.InvInputLineDao;
import com.erp.inv.input.dao.model.InvInputLine;
import com.erp.inv.input.dao.model.InvInputLineCO;

@Repository
public class InvInputLineDaoImpl implements InvInputLineDao{ 

    //注入DaoSupport工具类
    @Autowired
    private DaoSupport daoSupport;
    
    @Override
    public void insertDataObject(InvInputLine obj) {
        this.daoSupport.insertDataTransaction(obj);
    }

    @Override
    public void updateDataObject(InvInputLine obj) {
        this.daoSupport.updateDataTransaction(obj);
    }
    
    @Override
    public void insertOrUpdateDataObject(InvInputLine obj) {
        this.daoSupport.insertOrUpdateDataTransaction(obj);
    }

    @Override
    public void deleteDataObject(InvInputLine obj) {
        this.daoSupport.deleteDataTransactionJPA(obj);
    }

    @Override
    public List<InvInputLine> getDataObjects() {
        return this.daoSupport.getDataAllObject(InvInputLine.class);
    }

    @Override
    public InvInputLine getDataObject(int id) {
        return (InvInputLine)this.daoSupport.getDataObject(InvInputLine.class, id);
    }
    
    @Override
    public InvInputLine getDataObject(String code) {
        return null;
    }
    
    @Override
    public List<InvInputLine> getDataObjects(InvInputLineCO paramObj) {
        return null;
    }
    
    @Override
    public List<InvInputLine> getDataObjects(Pages pages) {
        return null;
    }
    
    @Override
    public List<InvInputLine> getDataObjects(Pages pages, InvInputLineCO paramObj) {
        return null;
    }

    @Override
    public List<Map<String, Object>> getDataObjectsArray(Pages pages, InvInputLineCO paramObj) {
        return null;
    }
    
    @Override
    @Permissions(PermissionType.DATA_AUTH)
    public List<InvInputLine> getDataObjectsForDataAuth(@SqlParam String dataAuthSQL, Pages pages, InvInputLineCO paramObj) {
        return null;
    }
    
    @Override
    public List<InvInputLine> getInvInputLineListByInputHeadCode(Pages pages, InvInputLineCO paramObj) {
        String sql = "select l.* from inv_input_line l where l.is_del = '0' and l.input_head_code=:inputheadcode";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("inputheadcode", paramObj.getInputHeadCode());
        sql = sql + " order by l.input_line_id";
        
        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("l", InvInputLine.class);
        
        return this.daoSupport.getDataSqlByPage(sql, entity, args, pages);
    }
    
    @Override
    public void deleteInvInputLineByInputHeadCode(String inputHeadCode) {
        String sql = "delete from inv_input_line where input_head_code = :inputheadcode";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("inputheadcode", inputHeadCode);
        
        this.daoSupport.executeSQLTransaction(sql, args);
    }
    
    @Override
    public BigDecimal getInputQuantityByPoLineCode(String poLineCode) {
        String sql = "select sum(l.input_quantity) from inv_input_line l,inv_input_head h "
                   + "where h.input_head_code = l.input_head_code and h.status = 'CONFIRM' and h.approve_status = 'APPROVE' "
                   + "and l.input_source_line_code = :poLineCode";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("poLineCode", poLineCode);
        
        List list = this.daoSupport.selectDataSqlCount(sql, args);
        if(list!=null&&list.size()>0) {
            if(list.get(0)!=null) {
                return new BigDecimal(String.valueOf(list.get(0)));
            }
        }
        
        return BigDecimal.ZERO;
    }
    
    @Override
    public List<InvInputLine> getInvInputLineListByInputHeadCode(String inputHeadCode) {
        String sql = "select l.* from inv_input_line l where input_head_code=:inputheadcode";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("inputheadcode", inputHeadCode);
        sql = sql + " order by l.input_line_id";
        
        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("l", InvInputLine.class);
        
        return this.daoSupport.selectDataSql(sql, entity, args);
    }
    
    @Override
    public BigDecimal getInputedQuantityByPoLine(String poLineCode, Integer inputLineId) {
        String sql = "select sum(l.input_quantity*(select (case when h.input_type='PO_RETURN' then -1 else 1 end) from inv_input_head h where h.input_head_code = l.input_head_code)) from inv_input_line l where l.input_source_line_code = :code and l.input_line_id <> :id";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("code", poLineCode);
        args.put("id", inputLineId);
        
        List list = this.daoSupport.selectDataSqlCount(sql, args);
        if(list!=null&&list.size()>0) {
            if(list.get(0)!=null) {
                return new BigDecimal(String.valueOf(list.get(0)));
            }
        }
        
        return BigDecimal.ZERO;
    }
    
    @Override
    public void updateLineForVersion(String code) {
        String sql = "update inv_input_line set version = version+1 where input_head_code = :code";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("code", code);
        
        this.daoSupport.executeSQLTransaction(sql, args);
    }

    @Override
    public List<InvInputLineVO> getInputLineAndMaterial(Pages pages, InvInputLineVO invInputLineVO, String supply_material_id){
        String sql = "select '1' as allPrice,'1' as loss,'1' as give,'1' as attached_memo,a.*,b.material_id,b.material_code as a_material_code,b.material_name,b.material_type,b.category_code,b.material_unit,b.brand as a_brand,b.valid_day,b.standard as a_standard,b.standard_unit as a_standard_unit,b.supplier,b.remark,b.pack_standard,b.bom_property,b.produce_pre_days,b.version,b.status,b.approve_status,b.is_del as a_is_del,b.created_date,b.created_by,b.last_updated_date,b.last_updated_by,b.org_code from supply_material_detail a, md_material b\n" +
                "where a.supply_material_id = b.material_id and a.supply_material_id = :supply_material_id";

        sql = sql + " order by a.create_time desc";
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("supply_material_id", supply_material_id);
        List<InvInputLineVO> result = daoSupport.getDataSqlByPage(sql,InvInputLineVO.class,args,pages);
        return result;
    }

}
